package org.leviatan.dataharbour.gui.swingcomponents.views;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import org.leviatan.dataharbour.gui.RuntimeContext;
import org.leviatan.dataharbour.gui.swingmodels.TreeMdlDataSource;
import org.leviatan.dataharbour.gui.swingmodels.TreeNodeMdlDataSource;

public class EditDataSourcePanel extends JPanel {

	private static final long serialVersionUID = 1966240046805569129L;
	
	private JLabel lblTitle;
	
	private JPanel butPanel;
	
	private JLabel lblName;
	private JTextField txtName;
	
	private JButton butUpdate;
	private JButton butCancel;
	
	public EditDataSourcePanel() {
		super();
		this.init();
	}
	
	private void init() {
		this.setLayout(new GridBagLayout());
		this.setVisible(true);
		
		
		// create new constraints
		///////////////////////////////////////////////////////////////////////
		GridBagConstraints lblTitleC = new GridBagConstraints();
		
		GridBagConstraints lblNameC = new GridBagConstraints();
		GridBagConstraints txtNameC = new GridBagConstraints();
		
		GridBagConstraints butPanelC = new GridBagConstraints();
		
		
		// setup constraints
		///////////////////////////////////////////////////////////////////////		
		
		lblTitleC.anchor = GridBagConstraints.NORTHWEST;
		lblTitleC.fill = GridBagConstraints.NONE;
		lblTitleC.gridx = 0;
		lblTitleC.gridy = 0;
		lblTitleC.gridwidth = 2;
		lblTitleC.insets = new Insets(10,5,20,5);
		
		
		lblNameC.fill = GridBagConstraints.NONE;
		lblNameC.gridx = 0;
		lblNameC.gridy = 1;
		lblNameC.insets = new Insets(5,5,5,5);
		
		txtNameC.fill = GridBagConstraints.NONE;
		txtNameC.gridx = 1;
		txtNameC.gridy = 1;
		txtNameC.insets = new Insets(5,5,5,5);
		
		
		butPanelC.anchor = GridBagConstraints.WEST;
		butPanelC.fill = GridBagConstraints.NONE;
		butPanelC.gridwidth = 2;
		butPanelC.gridx = 0;
		butPanelC.gridy = 2;
		butPanelC.weighty = 1.0;
		butPanelC.weightx = 1.0;
		//butPanelC.ipadx = 200;
		butPanelC.insets = new Insets(5,5,5,5);
		
		
		
		// init components
		///////////////////////////////////////////////////////////////////////
		
		this.lblTitle = new JLabel("Edit DataSource");
		
		this.lblName = new JLabel("Name:");
		
		TreeNodeMdlDataSource nodeMdl = ((TreeMdlDataSource) RuntimeContext.
				dataSourceTree.getModel()).getNodeModel(this.getSelectedDSId());
		
		
		this.txtName = new JTextField(nodeMdl.getName());
		this.txtName.setColumns(25);
				
		
		// add components
		///////////////////////////////////////////////////////////////////////
		
		this.add(lblTitle, lblTitleC);
		
		this.add(lblName, lblNameC);
		this.add(txtName, txtNameC);
		
		this.add(getButtonPanel(), butPanelC);
		
		
		// add listeners
		///////////////////////////////////////////////////////////////////////
		this.butUpdate.addMouseListener(new MouseAdapter() {
			
			public void mouseClicked(MouseEvent e) {
				String name = txtName.getText().trim();
				String dsId = EditDataSourcePanel.this.getSelectedDSId();
				
				if (dsId != null)
					RuntimeContext.dataHarbourService.updateDataSource(dsId, name);
				
				RuntimeContext.dataHarbourService.saveDataSources();
				((TreeMdlDataSource)RuntimeContext.dataSourceTree.getModel()).reload();
				RuntimeContext.editDataSourceFrame.dispose();
				
				// select the new ds
				TreeNodeMdlDataSource nodeMdl = ((TreeMdlDataSource)
						RuntimeContext.dataSourceTree.getModel()).getNodeModel(dsId);
				
				RuntimeContext.dataSourceTree.setSelectionPath(new TreePath(nodeMdl.getPath()));
			}
		});
		
		this.butCancel.addMouseListener(new MouseAdapter() {
			
			public void mouseClicked(MouseEvent e) {
				RuntimeContext.editDataSourceFrame.dispose();
			}
			
		});
	}
	
	private String getSelectedDSId() {
		
		DefaultMutableTreeNode selectedNode = 
			(DefaultMutableTreeNode) RuntimeContext.dataSourceTree.getLastSelectedPathComponent();

		if (selectedNode == null) return null;
	
		TreeNodeMdlDataSource dsNodeModel = 
			(TreeNodeMdlDataSource) selectedNode.getUserObject();
		
		return dsNodeModel.getId();
	}
	
	
	private JPanel getButtonPanel() {
		this.butPanel = new JPanel();
		this.butPanel.setLayout(new GridLayout());
		
		GridBagConstraints butUpdateC = new GridBagConstraints();
		GridBagConstraints butCancelC = new GridBagConstraints();
		
		//butSaveC.anchor = GridBagConstraints.WEST;
		butUpdateC.fill = GridBagConstraints.NONE;
		butUpdateC.insets = new Insets(5,5,5,5);
		butUpdateC.gridx = 0;
		butUpdateC.gridy = 0;
		butUpdateC.ipadx = 10;
		
		butCancelC.fill = GridBagConstraints.NONE;
		butCancelC.insets = new Insets(5,5,5,5);
		butCancelC.gridx = 0;
		butCancelC.gridy = 0;
		butCancelC.ipadx = 10;
		
		
		this.butUpdate = new JButton("Update");
		this.butCancel = new JButton("Cancel");
		
		this.butPanel.add(butUpdate, butUpdateC);
		this.butPanel.add(butCancel, butCancelC);
		
		return this.butPanel;
	}

}
